package org.jeecg.modules.bicycle.vehicle.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.bicycle.order.entity.BikeVehicleOrder;
import org.jeecg.modules.bicycle.order.mapper.BikeVehicleOrderMapper;
import org.jeecg.modules.bicycle.vehicle.entity.BikeVehicle;
import org.jeecg.modules.bicycle.vehicle.mapper.BikeVehicleMapper;
import org.jeecg.modules.bicycle.vehicle.service.IBikeVehicleService;
import org.jeecg.modules.bicycle.vehicle.vo.BikeCard;
import org.jeecg.modules.bicycle.vehicle.vo.BikeVehicleInitialization;

import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.area.service.IBikeSysAreaService;
import org.jeecg.modules.param.pile.entity.BikePile;
import org.jeecg.modules.param.pile.mapper.BikePileMapper;
import org.jeecg.modules.param.station.entity.BikeStation;
import org.jeecg.modules.param.station.mapper.BikeStationMapper;
import org.jeecg.modules.utils.EstUtils;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.utils.YueYueNiaoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: bike_vehicle
 * @Author: 月月鸟
 * @Date:   2021-05-19
 * @Version: V1.0
 */
@Service
public class BikeVehicleServiceImpl extends ServiceImpl<BikeVehicleMapper, BikeVehicle> implements IBikeVehicleService {
    @Resource
    private BikeVehicleMapper bikeVehicleMapper;
    @Resource
    private BikeStationMapper bikeStationMapper;
    @Resource
    private BikePileMapper bikePileMapper;
    @Resource
    private BikeVehicleOrderMapper bikeVehicleOrderMapper;
    @Autowired
    private IBikeSysAreaService bikeSysAreaService;
    @Override
    public Page<BikeVehicle> pageList( Page<BikeVehicle> page,BikeVehicle bikeVehicle) {
        List<BikeVehicle> bikeVehicles = bikeVehicleMapper.pageList(page,bikeVehicle);

        return page.setRecords(bikeVehicles);
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request, BikeVehicle bikeVehicle) {

        List<BikeVehicle> bikeVehicles = bikeVehicleMapper.list(bikeVehicle);

        return ExcelUtils.exportXls(request,bikeVehicles,BikeVehicle.class,"车辆管理");
    }

    @Override
    public Result<?> initialization(BikeVehicleInitialization bikeVehicle) {
        String bike = RandomCodeUtils.getRandomCode("bike");
        BikeVehicle vehicle = new BikeVehicle();
        //传code，存code
        if (bikeVehicle.getCode()==null){
            vehicle.setCode(bike);
            bikeVehicleMapper.insert(vehicle);
            return Result.OK(bike);
        }else {
            QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
            wrapper.eq("code",bikeVehicle.getCode());
            BikeVehicle vehicleData = bikeVehicleMapper.selectOne(wrapper);
            //不为空则修改 空则添加
            if (vehicleData!=null){
                vehicleData.setCode(bike);
                bikeVehicleMapper.updateById(vehicleData);
                return Result.OK(bike);
            }else {
                vehicle.setCode(bike);
                bikeVehicleMapper.insert(vehicle);
                return Result.OK(bike);
            }
        }
    }

    @Override
    public Result<?> addCard(BikeCard addCard) {
        return null;
    }

    @Override
    public Result<?> delCard(BikeCard addCard) {
        if (StringUtils.isEmpty(addCard.getCode())){
            return Result.error("未获取卡号，请重试");
        }
        String code = EstUtils.hexAddCrc8ToAscii(addCard.getCode());
        QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
        wrapper.eq("code", code);
        BikeVehicle vehicleData = bikeVehicleMapper.selectOne(wrapper);
        if (vehicleData!=null){
            vehicleData.setIsRegister(BikeConstant.y_n_no);
            bikeVehicleMapper.update(vehicleData,wrapper);
        }
        return Result.OK("清卡成功");
    }

    @Override
    public Result<?> changeCard(BikeCard changeCard) {
        String code = EstUtils.hexAddCrc8ToAscii(changeCard.getCode());
        if (StringUtils.isNotEmpty(code)){
            QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
            wrapper.eq("code",code);
            BikeVehicle vehicle = bikeVehicleMapper.selectOne(wrapper);
            vehicle.setIsRegister(BikeConstant.y_n_yes);
            bikeVehicleMapper.updateById(vehicle);
            return Result.OK("换卡成功");
        }else {
            return Result.error("未收到卡片编号");
        }

    }

    @Override
    public Result<?> getByCode(String code) {
        QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
        wrapper.select("code");
        if (StringUtils.isNotEmpty(code)){
            wrapper.like("code",code);
        }
        wrapper.eq("type", 2);
        List<BikeVehicle> bikeVehicles = bikeVehicleMapper.selectList(wrapper);
        return Result.OK(bikeVehicles);
    }
    //获取车桩id
    @Override
    public String getPileId(BikeVehicle tb) {
        //获取网点信息
        if (StringUtils.isEmpty(tb.getStationCode())||StringUtils.isEmpty(tb.getPileCode())){
            return null;
        }
        QueryWrapper<BikeStation> bs = new QueryWrapper<>();
        bs.eq("code", tb.getStationCode());
        BikeStation bikeStation = bikeStationMapper.selectOne(bs);
        if (bikeStation != null) {
            //通过网点信息获取车桩信息
            QueryWrapper<BikePile> wrapper = new QueryWrapper<>();
            wrapper.eq("station_id", bikeStation.getId());
            wrapper.eq("code", tb.getPileCode());
            BikePile bikePile = bikePileMapper.selectOne(wrapper);
            if (bikePile != null) {
                //返回车桩id
                return bikePile.getId();
            }else {
                return null;
            }
        }
        return null;
    }

    @Override
    public Result<?> deleteCheck(String id) {
        BikeVehicle vehicle = bikeVehicleMapper.selectById(id);
        if (vehicle!=null){
            if (StringUtils.isNotEmpty(vehicle.getIsRegister())){
                if (vehicle.getIsRegister().equals(BikeConstant.y_n_yes)){
                    return Result.error("已发卡，无法删除");
                }
            }
            QueryWrapper<BikeVehicleOrder> bvo = new QueryWrapper<>();
            bvo.eq("vehicle_id",vehicle.getId());
            List<BikeVehicleOrder> bikeVehicleOrders = bikeVehicleOrderMapper.selectList(bvo);
            if (bikeVehicleOrders.size()!=0){
                return Result.error("车辆已营运，无法删除");
            }
            return Result.OK1();
        }else {
            return Result.error("未找到车辆信息，请重试");
        }

    }

    //@Override
    //public Result<?> getBikeCode() {
    //    QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
    //    wrapper.eq("is_register",BikeConstant.y_n_no);
    //    List<BikeVehicle> bikeVehicles = bikeVehicleMapper.selectList(wrapper);
    //    if (bikeVehicles.size()==0){
    //        return Result.error("没有数据可以发卡");
    //    }
    //    BikeVehicle vehicle = bikeVehicles.get(0);
    //    String s = EstUtils.asciiToHexAddCrc8(vehicle.getCode());
    //    return Result.OK(s);
    //}

    @Override
    public Result<?> updateBikeCode(String code) {
        String s = EstUtils.hexAddCrc8ToAscii(code);
        //查询所有 未注册数据
        BikeVehicle selectData = new BikeVehicle();
        selectData.setIsRegister(BikeConstant.y_n_no);
        List<BikeVehicle> bikeVehiclesData = bikeVehicleMapper.list(selectData);
        //修改
        QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
        wrapper.eq("code",s);
        BikeVehicle vehicle = bikeVehicleMapper.selectOne(wrapper);
        vehicle.setIsRegister(BikeConstant.y_n_yes);
        bikeVehicleMapper.updateById(vehicle);

        //数据筛选到指定id
        List<BikeVehicle> bikeVehicles1 = YueYueNiaoUtils.deleteFrontDataById(bikeVehiclesData, vehicle.getId());
        if (bikeVehicles1.isEmpty()){
            return Result.error("此组数据已发完");
        }
        return Result.OK(bikeVehicles1.get(0));
    }
    @Override
    public boolean checkCode( String code) {
        Integer selectCount = bikeVehicleMapper.selectCount(new QueryWrapper<BikeVehicle>().select("id").eq("code", code).eq("is_register","1"));
        return selectCount == 0;

    }

    @Override
    public Result<?> clearCard(BikeVehicle bikeVehicle) {
        if (StringUtils.isNotEmpty(bikeVehicle.getCode())){
            String code = EstUtils.hexAddCrc8ToAscii(bikeVehicle.getCode());
            QueryWrapper<BikeVehicle> wrapper = new QueryWrapper<>();
            wrapper.eq("code",code);
            wrapper.select("id","code");
            BikeVehicle vehicle = bikeVehicleMapper.selectOne(wrapper);
            if (vehicle!=null){
                vehicle.setIsRegister(BikeConstant.y_n_no);
                bikeVehicleMapper.updateById(vehicle);
                return Result.OK("清卡成功");
            }else {
                return Result.OK("清卡成功");
            }
        }else {
            return Result.error("无需清卡");
        }

    }


}
